package com.xywy.bloodandroid.act;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

/**
 * 设置新密码
 */
public class ChangeNewPasswordActivity extends BaseActivity {

	private TextView middle_title; // title
	private RelativeLayout left_img; // left click
	private EditText et_new_pwd; // new password
	private EditText et_comfirm_pwd; // confirm new password
	private RelativeLayout rl_comfirm_change; // change pwd click

	private TextView chenck_password_text;
	private ImageView chenck_newpwd_img;
	private ImageView chenck_comfirm_newpwd;
	private Handler handler = new Handler();

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_new_pwd);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("设置新密码");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
		et_comfirm_pwd = (EditText) findViewById(R.id.et_comfirm_pwd);
		rl_comfirm_change = (RelativeLayout) findViewById(R.id.rl_comfirm_change);
		chenck_password_text = (TextView) findViewById(R.id.chenck_password_text);
		chenck_newpwd_img = (ImageView) findViewById(R.id.chenck_newpwd_img);
		chenck_comfirm_newpwd = (ImageView) findViewById(R.id.chenck_comfirm_newpwd);
		signPWD();
		rl_comfirm_change.setClickable(false);
		rl_comfirm_change.setBackgroundResource(R.drawable.login_button_press);

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		rl_comfirm_change.setOnClickListener(this);
	}

	Dialog dialog;
	EditText et_sign_pwd;
	Button bt_cancle;
	Button bt_comfirm;

	/**
	 * 验证原密码的对话框
	 */
	private void signPWD() {

		dialog = new Dialog(this);
		dialog.setTitle("验证密码");
		dialog.setContentView(R.layout.dialog_sign_pwd);
		et_sign_pwd = (EditText) dialog.findViewById(R.id.et_sign_pwd);
		bt_cancle = (Button) dialog.findViewById(R.id.bt_cancle);
		bt_comfirm = (Button) dialog.findViewById(R.id.bt_comfirm);
		bt_cancle.setOnClickListener(this);
		bt_comfirm.setOnClickListener(this);
		dialog.setCancelable(false);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();

	}

	@Override
	protected void initData() {
		showToast("进入页面的时候会弹出一个对话框，验证原密码");
		et_new_pwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (et_new_pwd.getText().toString().trim().length() > 0) {
					chenck_password_text.setVisibility(View.GONE);
				} else
					chenck_password_text.setVisibility(View.VISIBLE);

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		et_comfirm_pwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (et_comfirm_pwd.getText().toString().trim().length() >= 6) {
					if (et_new_pwd.getText().toString().trim()
							.equals(et_comfirm_pwd.getText().toString().trim())) {
						chenck_newpwd_img.setVisibility(View.VISIBLE);
						chenck_comfirm_newpwd.setVisibility(View.VISIBLE);
						rl_comfirm_change.setClickable(true);
						rl_comfirm_change
								.setBackgroundResource(R.drawable.login_button_selector);
					} else {
						chenck_newpwd_img.setVisibility(View.GONE);
						chenck_comfirm_newpwd.setVisibility(View.GONE);
						rl_comfirm_change.setClickable(false);
						rl_comfirm_change
								.setBackgroundResource(R.drawable.login_button_press);
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			ChangeNewPasswordActivity.this.finish();
			break;

		case R.id.rl_comfirm_change:
			if (et_comfirm_pwd.getText().toString().trim().length() >= 6) {
				submit(et_comfirm_pwd.getText().toString().trim());
			}
			break;

		case R.id.bt_cancle:
			ChangeNewPasswordActivity.this.finish();
			dialog.dismiss();
			break;
		case R.id.bt_comfirm:
			// 验证原始密码； 如果相同则进入，如果不同，则提示错误
			if (User.getInstance(this).getPassword()
					.equals(et_sign_pwd.getText().toString().trim())) {
				dialog.dismiss();
				showToast("验证成功");
			} else {
				showToast("验证失败");
			}

			break;
		}
	}

	/**
	 * 修改密码；
	 */
	private void submit(String num) {

		String sign = null;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		String url = "http://api.wws.xywy.com/index.php?act=kbb&fun=resetProperty&"
				+ "tag=" + Constants.tag + "&sign=" + sign;

		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						showToast("修改密码失败！");
					}

					@Override
					public void onSuccess(String response) {
						try {
							JSONObject jsonObject = new JSONObject(response);
							int state = jsonObject.getInt("state");
							if (state == 200) {
								showToast("修改密码成功！");
								finish();
							} else {
								showToast("修改密码失败！");
							}

						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

		Map<String, String> params = new HashMap<String, String>();
		params.put("userid", User.getInstance(context).getUserid());
		params.put("keyword", "password");
		params.put("pushable", "1");
		params.put("value", num);
		params.put("type", "3");

		postRequest.setIsParseJson(false);
		postRequest.setParams(params);
		VolleyManager.addRequest(postRequest, "");

	}
}
